﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Common
{
    public class SendSmsHelper
    {
        private static string MSAccount = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["MSSendUserName"]) ? "JSM41181" : System.Configuration.ConfigurationManager.AppSettings["MSSendUserName"];
        private static string MSPass = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["MSSendPassword"]) ? "7bkvbt2v" : System.Configuration.ConfigurationManager.AppSettings["MSSendPassword"];
        private static string MSVeryCode = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["MSSendVeryCode"]) ? "9ojwem4gh7d5" : System.Configuration.ConfigurationManager.AppSettings["MSSendVeryCode"];
        private static string MSHTTP_URL = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["MSSendHTTP_URL"]) ? "http://112.74.76.186:8030/service/httpService/httpInterface.do?method=" : System.Configuration.ConfigurationManager.AppSettings["MSSendHTTP_URL"];
        /// <summary>
        ///  发送美圣短信
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string SendSmsMeiSheng(string Phone, string Content, string ModuleID)
        {
            //新的发送短信方法
            StringBuilder param = new StringBuilder();
            param.Append("&username=").Append(MSAccount);
            param.Append("&password=").Append(MSAccount);
            //通信认证密码
            param.Append("&veryCode=").Append(MSVeryCode);
            param.Append("&mobile=").Append(Phone);
            param.Append("&content=").Append(Content);
            //param.Append("&tempid=").Append(ModuleID);
            //短信类型。1-普通短信，2-模板短信。
            param.Append("&msgtype=").Append("1");
            param.Append("&code=").Append("utf-8");
            //发送定时短信
            //param.Append("&sendtime=").Append("20151001113000"); 

            String address = String.Format("{0}sendMsg{1}", MSHTTP_URL, param.ToString());

            //发送短信，并获取发送状态
            return GetPagePost(address);//address

            #region 老的发送方法
            //String smscontent = System.Web.HttpUtility.UrlEncode(Content, System.Text.Encoding.GetEncoding("gbk"));
            //String url = "http://mssms.cn:8000/msm/sdk/http/sendsms.jsp?username=" + MSAccount + "&scode=" + MSPass + "&tempid=" + ModuleID + "&content=" + smscontent + "&mobile=" + Phone;

            //Common.HttpClient client = new HttpClient(url);

            //return client.GetString();
            #endregion
        }

        /// <summary>
        ///  发送美圣短信
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string SendSmsMeiSheng(string Phone, string Content, string ModuleID, string signtag)
        {
            //新的发送短信方法
            StringBuilder param = new StringBuilder();
            param.Append("&username=").Append(MSAccount);
            param.Append("&password=").Append(MSPass);
            //通信认证密码
            param.Append("&veryCode=").Append(MSVeryCode);
            param.Append("&mobile=").Append(Phone);
            param.Append("&content=").Append(Content);
            param.Append("&tempid=").Append(ModuleID);
            //短信类型。1-普通短信，2-模板短信。
            param.Append("&msgtype=").Append("2");
            param.Append("&code=").Append("utf-8");
            //发送定时短信
            //param.Append("&sendtime=").Append("20151001113000"); 

            String address = String.Format("{0}sendMsg{1}", MSHTTP_URL, param.ToString());

            //发送短信，并获取发送状态
            return GetPagePost(address);//address



            #region 老的短信发送方法
            //if (signtag == "")
            //    signtag = "1";

            //String smscontent = System.Web.HttpUtility.UrlEncode(Content, System.Text.Encoding.GetEncoding("gbk"));
            //String url = "http://mssms.cn:8000/msm/sdk/http/sendsms.jsp?username=" + MSAccount + "&scode=" + MSPass + "&tempid=" + ModuleID + "&content=" + smscontent + "&mobile=" + Phone + "&signtag=" + signtag;

            //Common.HttpClient client = new HttpClient(url);

            //return client.GetString();
            #endregion
        }




        /// <summary>
        /// POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetPagePost(string url)
        {

            HttpWebRequest httpReq;
            HttpWebResponse httpResp;

            StringBuilder strBuff = new StringBuilder();

            int byteRead = 0;
            int byteCount = 256;

            char[] cbuffer = new char[byteCount];

            try
            {

                Uri httpURL = new Uri(url);
                httpReq = (HttpWebRequest)WebRequest.Create(httpURL);

                httpReq.Method = "POST";
                httpResp = (HttpWebResponse)httpReq.GetResponse();
                Stream respStream = httpResp.GetResponseStream();
                // httpResp.Dispose();
                //StreamReader类的Read方法依次读取网页源程序代码每一行的内容，直至行尾（读取的编码格式：UTF8） 
                StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
                byteRead = respStreamReader.Read(cbuffer, 0, byteCount);
                while (byteRead != 0)
                {
                    string strResp = new string(cbuffer, 0, byteRead);
                    strBuff.Append(strResp);
                    byteRead = respStreamReader.Read(cbuffer, 0, byteCount);
                }


            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {

            }

            return strBuff.ToString();
        }
    }
}
